package cc.yugu.yhdw.webserver.service.hdw;

import cc.yugu.yhdw.common.domain.hdw.Rescuer;
import cc.yugu.yhdw.common.entity.AreaCache;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.pagination.Page;
import cc.yugu.yhdw.common.service.AbstractService;
import cc.yugu.yhdw.webserver.config.AppConfig;
import cc.yugu.yhdw.webserver.persistence.hdw.RescuerMapper;
import com.whalin.MemCached.MemCachedClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 移动救援业务
 * 
 * @Date:Created in 2018/7/10 16:54
 * @Author:
 * @Modified By: zjn 2018/7/10 16:54
 */ 
@Service
public class RescuerService extends AbstractService {

    @Autowired
    MemCachedClient memCachedClient;
    @Autowired
    AppConfig config;
    @Autowired
    RescuerMapper rescuerMapper;
    @Autowired
    AreaCache areaCache;
    /**
    * 查询地址信息
    * 
    * @Param: [id]
    * @Return: cc.yugu.yhdw.common.domain.hdw.Rescuer
    * @Author By: zjn 2018/7/10  16:56
    */ 
    public Rescuer find(int id) {
        return (Rescuer) setAreaProperties(areaCache, rescuerMapper.find(id));
    }
    /**
    * 根据运营商id查询
    * 
    * @Param: [agent]
    * @Return: java.util.List<cc.yugu.yhdw.common.domain.hdw.Rescuer>
    * @Author By: zjn 2018/7/10  16:56
    */ 
    public List<Rescuer> findByAgent(int agent) {
        return rescuerMapper.findByAgent(agent);
    }
    /**
    * 分页查询
    * 
    * @Param: [search]
    * @Return: cc.yugu.yhdw.common.entity.pagination.Page
    * @Author By: zjn 2018/7/10  16:57
    */ 
    public Page findPage(Rescuer search) {
        Page page = search.buildPage();
        page.setTotalItems(rescuerMapper.findPageCount(search));
        search.setBeginIndex(page.getOffset());
        List<Rescuer> list = rescuerMapper.findPageResult(search);
        page.setResult(setAreaProperties(areaCache, list));
        return page;
    }
    /**
    * 插入
    * 
    * @Param: [rescuer]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/10  17:09
    */ 
    public ExtResult create(Rescuer rescuer) {
        try {
            rescuerMapper.insert(rescuer);
        } catch (Exception e) {
            return ExtResult.failResult("操作失败");
        }
        return ExtResult.successResult();
    }
    /**
    * 更新
    * 
    * @Param: [rescuer]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/10  17:09
    */ 
    public ExtResult update(Rescuer rescuer) {
        try {
            rescuerMapper.update(rescuer);
        } catch (Exception e) {
            return ExtResult.failResult("操作失败");
        }
        return ExtResult.successResult();
    }
    /**
    * 删除
    * 
    * @Param: [id]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/10  17:10
    */ 
    public ExtResult delete(int id) {
        try {
            rescuerMapper.delete(id);
        } catch (Exception e) {
            return ExtResult.failResult("操作失败");
        }
        return ExtResult.successResult();
    }
}
